# Author: Mark Richardson
# Purpose: Create figures in the main text

# Load packages
library(dplyr)
library(ggplot2)
library(stringr)

# Load data
load("data/05_dis_data_w_traits.RData")


#### Figure 1 ####

# Create the agency ideology factor variable and index for plotting

dis_data_wt <- dis_data_wt %>%
  mutate(ideo_cat = case_when(ideo_rating < 0 & ideo_ub < 0 ~ "Liberal",
                              ideo_rating > 0 & ideo_lb > 0 ~ "Conservative",
                              TRUE ~ "Moderate")) %>%
  mutate(ideo_cat = if_else(is.na(ideo_rating), "No measure", ideo_cat),
         ideo_cat = factor(ideo_cat, levels = c("Conservative", "Moderate", "Liberal", "No measure"))) %>%
  arrange(pty_dis) %>%
  mutate(plot_index = 1:n())

# Adjust agency names for plotting

dis_data_wt <- dis_data_wt %>%
  mutate(agency_plot = str_replace_all(agency, "United States", "U.S."),
         agency_plot = if_else(!is.na(dept_acr), paste0(agency_plot, " (", dept_acr, ")"), agency_plot),
         agency_plot = str_replace_all(agency_plot, "Office of the Secretary", "Office of the Sec."))




# Subset to the agencies with the 15 lowest and 15 highest posterior means

dis_plot <- dis_data_wt %>%
  filter(plot_index <= 15 | plot_index >= 79 ) %>%
  arrange(plot_index) %>%
  mutate(plot_index = 1:n())

# Code for figure 1

ggplot(dis_plot) +
  geom_point(aes(pty_dis, plot_index, shape = ideo_cat, color = ideo_cat), size = 2, alpha = 1) +
  geom_segment(aes(x = pty_dis_lb_95, xend = pty_dis_ub_95,
                   y = plot_index, yend = plot_index,
                   color = ideo_cat), alpha = 0.5) +
  geom_text(aes(x = -5.5, y = plot_index,
                label = agency_plot,
                hjust = 1)) +
  theme_bw() +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.x = element_text(size = 12)) +
  labs(x = "Latent Disagreement",
       y = "",
       color = "Agency Ideology",
       shape = "Agency Ideology",
       caption = "Horizontal lines are the 95% Bayesian credible interval.") +
  scale_x_continuous(breaks = -4:4, limits = c(-17, 6), sec.axis = dup_axis()) +
  scale_shape_manual(values = 15:18) +
  scale_color_manual(values = c("firebrick2", "gray57", "dodgerblue", "black")) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_hline(yintercept = c(15.5))



#### Figure 2 ####

scatter <- ggplot(dis_data_wt %>% filter(!is.na(ideo_rating)), aes(ideo_rating, pty_dis)) +
  geom_point(aes(color = ideo_cat, shape = ideo_cat)) +
  geom_smooth(method = "loess", se = FALSE, color = "gray") +
  geom_text(aes(label = "EPA"), data = dis_data_wt %>% filter(agency == "Environmental Protection Agency"),
            nudge_x = 0.2) +
  geom_text(aes(label = "Peace\nCorps"), data = dis_data_wt %>% filter(agency == "Peace Corps"),
            nudge_y = -0.3) +
  geom_text(aes(label = "NIH"), data = dis_data_wt %>% filter(agency == "National Institutes of Health"),
            nudge_y = -0.15) +
  geom_text(aes(label = "CMS"), data = dis_data_wt %>% filter(agency == "Centers for Medicare and Medicaid Services"),
            nudge_x = 0.15, nudge_y = -0.15) +
  geom_text(aes(label = "Army,\nNavy, &\n USAF"), data = dis_data_wt %>% filter(agency == "Army"),
            nudge_x = -0.15, nudge_y = -0.7) +
  geom_text(aes(label = "ATF"), data = dis_data_wt %>% filter(agency == "Bureau of Alcohol, Tobacco, Firearms and Explosives"),
            nudge_x = 0.20, nudge_y = 0) +
  geom_text(aes(label = "USCBP"), data = dis_data_wt %>% filter(agency == "United States Customs and Border Protection"),
            nudge_x = -0.35, nudge_y = 0) +
  geom_text(aes(label = "USCIS"), data = dis_data_wt %>% filter(agency == "United States Citizenship and Immigration Services"),
            nudge_x = 0.3, nudge_y = 0) +
  geom_text(aes(label = "Coast\nGuard"), data = dis_data_wt %>% filter(agency == "Coast Guard"),
            nudge_x = -0.3, nudge_y = 0) +
  geom_text(aes(label = "BFS"), data = dis_data_wt %>% filter(agency == "Bureau of the Fiscal Service"),
            nudge_x = -0.2, nudge_y = 0) +
  geom_text(aes(label = "BDS"), data = dis_data_wt %>% filter(agency == "Bureau of Diplomatic Security"),
            nudge_x = 0.25, nudge_y = 0) +
  geom_text(aes(label = "USPTO"), data = dis_data_wt %>% filter(agency == "U.S. Patent and Trademark Office"),
            nudge_x = 0.2, nudge_y = -0.15) +
  geom_text(aes(label = "CFPB"), data = dis_data_wt %>% filter(agency == "Consumer Financial Protection Bureau"),
            nudge_x = 0.025, nudge_y = 0.15) +
  geom_text(aes(label = "VBA"), data = dis_data_wt %>% filter(agency == "Veterans Benefits Administration"),
            nudge_x = 0.25, nudge_y = 0) +
  geom_text(aes(label = "NARA"), data = dis_data_wt %>% filter(agency == "National Archives and Records Administration"),
            nudge_x = -0.3, nudge_y = 0) +
  geom_text(aes(label = "NIST"), data = dis_data_wt %>% filter(agency == "National Institute of Standards and Technology"),
            nudge_x = 0.25, nudge_y = 0) +
  geom_text(aes(label = "NTSB"), data = dis_data_wt %>% filter(agency == "National Transportation Safety Board"),
            nudge_x = -0.25, nudge_y = 0.1) +
  geom_text(aes(label = "IRS"), data = dis_data_wt %>% filter(agency == "Internal Revenue Service"),
            nudge_x = 0, nudge_y = 0.15) +
  geom_text(aes(label = "NOAA"), data = dis_data_wt %>% filter(agency == "National Oceanic and Atmospheric Administration"),
            nudge_x = 0, nudge_y = -0.15) +
labs(x = "Agency Ideology", y = "Partisan Disagreement",
     shape = "Agency Ideology", color = "Agency Ideology",
     caption = "Line is a LOESS line.") +
  scale_shape_manual(values = 15:18) +
  scale_color_manual(values = c("firebrick2", "gray57", "dodgerblue")) +
  theme_bw()

scatter


